package com.itheima.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.base.R;
import com.itheima.pinda.base.entity.SuperEntity;
import com.itheima.wms.common.service.CrudServiceImpl;
import com.itheima.wms.common.utils.RelationUtils;
import com.itheima.wms.dao.LocationMapper;
import com.itheima.wms.dao.OwnerLocationMapper;
import com.itheima.wms.dao.StockMapper;
import com.itheima.wms.entity.AreaEntity;
import com.itheima.wms.entity.LocationEntity;
import com.itheima.wms.entity.OwnerLocationEntity;
import com.itheima.wms.service.AreaService;
import com.itheima.wms.service.LocationService;
import com.itheima.wms.vo.ImportResultVO;
import com.itheima.wms.vo.LocationDetailVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 业务实现类
 * 库位
 * </p>
 */
@Slf4j
@Service
public class LocationServiceImpl extends CrudServiceImpl<LocationMapper, LocationEntity> implements LocationService {

    private static List<Class> SERVICE = new ArrayList() {{
        add(StockMapper.class);
    }};

    @Autowired
    private AreaService areaService;
    @Autowired
    private OwnerLocationMapper ownerLocationMapper;

    @Override
    public List<LocationEntity> list(Map data) {
        if (data.containsKey("ownerId")) {
            Long ownerId = Long.valueOf(data.get("ownerId").toString());
            LambdaQueryWrapper<OwnerLocationEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OwnerLocationEntity::getOwnerId, ownerId);
            List<OwnerLocationEntity> ownerLocationEntities = ownerLocationMapper.selectList(wrapper);
            Set<Long> locationIdSet = ownerLocationEntities.stream().map(ownerLocationEntity -> ownerLocationEntity.getLocationId()).collect(Collectors.toSet());
            if (CollectionUtils.isEmpty(locationIdSet)) {
                return new ArrayList<>(0);
            }
            data.put("in_id", locationIdSet);
            data.remove("ownerId");
        }
        return super.list(data);
    }

    @Override
    public boolean save(LocationEntity entity) {
        boolean result = super.save(entity);
        if (result) {
            updateAreaCount(entity);
        }
        return result;
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        idList.forEach(id -> {
            RelationUtils.checkRelation(id, "location_id", SERVICE);

            LocationEntity entity = super.getById(id);
            updateAreaCount(entity);
        });
        return super.removeByIds(idList);
    }


    /**
     * 修改库区的库位数量
     *
     * @param locationEntity
     * @return
     */
    private boolean updateAreaCount(LocationEntity locationEntity) {
        int count = this.count(getWrapper(LocationEntity::getAreaId, locationEntity.getAreaId()));
        LambdaUpdateWrapper<AreaEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AreaEntity::getIncludedNum, count);
        updateWrapper.eq(SuperEntity::getId, locationEntity.getAreaId());
        return areaService.update(updateWrapper);
    }

    @SneakyThrows
    @Override
    public R<Map> upload(MultipartFile file) {
        return null;
    }

    @Override
    public ImportResultVO importExcel(MultipartFile file) {
        return null;
    }

    @Override
    public IPage<LocationDetailVO> pageDetail(Page<LocationDetailVO> page, Map data) {
        data.put("descs", "wl.create_time");
        IPage<LocationDetailVO> iPage = this.baseMapper.selectLocationDetail(page, data);
        return iPage;
    }

    @Override
    public List<LocationDetailVO> listDetail(Map data) {
        data.put("ascs", "wl.code");
        return this.baseMapper.selectLocationDetail(data);
    }
}

